#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
int main()
{
	float width,height;
	width = 600;
	height = 600;
	initgraph(width,height);
	float r,ball_x,ball_y,ball_vy,g;
	r = 20;/*小球半径*/
	ball_x=width/4;/*小球x位置*/
	ball_y=height/2;/*小球y位置*/
	ball_vy=0;/*小球初始速度*/
	g=0.5;/*重力加速度*/
	int HP = 10;//定义分数这一变量


	float rect_left_x,rect_top_y,rect_width,rect_height;/*定义长方形*/
	float rect_vx;/*定义方块速度*/
	rect_left_x=width*3/4;/*方块左边初始位置*/
	rect_height=100;/*方块高度*/
	rect_top_y=height-rect_height;/*方块上边y坐标*/
	rect_width=40;/*方块宽度*/
	rect_vx=-5;/*方块速度*/

	while(HP>0)/*循环语句*/
	{
		if (kbhit())/*当按键时*/
		{
			char c=_getch();/*获得输入字符*/
			if (c=' ')/*当按下空格键时*/
			{
				ball_vy=-10;/*给小球一个向上的初速度*/
			}
		}
		ball_vy=ball_vy+g;/*根据重力加速度更新小球的y速度*/
		ball_y=ball_y+ball_vy;/*根据小球的速度更新小球的y位置*/
		if (ball_y>=height-r)/*如果小球落到地面上*/
		{
			ball_y=height-r;/*避免小球落到地面下*/
			ball_vy=0;/*使其速度为0*/
		}
		if (ball_y<=20)/*如果小球碰到天花板*/
		{
			ball_y=r;/*避免小球飞出界面*/
			ball_vy=0;/*控制小球速度为0*/
		}
		rect_left_x=rect_left_x+rect_vx;/*给方块一个初始向左的速度*/
		if ((rect_left_x<=-10)&&(rect_left_x+300<=-10))/*如果方块跑到最左边*/
		{
			rect_left_x=width;/*方块回到最右边*/
			rect_height = rand() % int(height/4)+ height/4;
			rect_vx = rand()/float(RAND_MAX)*4-6;
		}
		if ((rect_left_x<=ball_x+r)&&(rect_left_x+rect_width>=ball_x-r)&&(height-rect_height<=ball_y+r))/*当方块碰到小球时*/
		{
			HP = HP-0.002;
			Sleep(100);/*速度放慢*/
		}
		if ((rect_left_x+300<=ball_x+r)&&(rect_left_x+rect_width+300>=ball_x-r)&&(height-rect_height<=ball_y+r))/*当方块碰到小球时*/
		{
			HP = HP-0.002;
			Sleep(100);/*速度放慢*/
		}
		if ((rect_left_x<=ball_x+r)&&(rect_left_x+rect_width>=ball_x-r)&&(height-rect_height-200>=ball_y-r))/*当方块碰到小球时*/
		{
			HP = HP-0.002;
			Sleep(100);/*速度放慢*/
		}
		if ((rect_left_x+300<=ball_x+r)&&(rect_left_x+rect_width+300>=ball_x-r)&&(height-rect_height-200>=ball_y-r))/*当方块碰到小球时*/
		{
			HP = HP-0.002;
			Sleep(100);/*速度放慢*/
		}

		cleardevice();/*清空画面*/
		fillrectangle(rect_left_x,height-rect_height,rect_left_x+rect_width,height);
		fillrectangle(rect_left_x,0,rect_left_x+rect_width,height-rect_height-200);
		fillrectangle(rect_left_x+300,height-rect_height,rect_left_x+300+rect_width,height);
		fillrectangle(rect_left_x+300,0,rect_left_x+300+rect_width,height-rect_height-200);
		fillcircle(ball_x,ball_y,r);/*绘制小球*/
		TCHAR s[50];/*定义字符串数组*/
		_stprintf(s,_T("%d"),HP);
		settextstyle(40,0,_T("宋体"));
		outtextxy(50,30,s);
		Sleep(10);/*暂停10毫秒*/

	}
	printf("游戏失败游戏失败游戏失败游戏失败游戏失败\n游戏失败游戏失败游戏失败游戏失败\n游戏失败游戏失败游戏失败游戏失败");
	system("pause");
	closegraph;
	_getch();
	return 0;
}